<?php

namespace app\model;

use think\Model;
use app\model\BalanceModel;
use think\Db;
class StudentModel extends Model{


    protected $table = 'mm_students';

    //添加
    public function add($data){
        $data['account_id'] = $data['admin_auth']['auth_admin_id'];
        $data['account_name'] = $data['admin_auth']['nickname'];
        $data['cid'] = $data['admin_auth']['cid'];
        $data['company_name'] = Db::name("company")->where("id",$data['admin_auth']['cid'])->value("company_name");
        unset($data['admin_auth']);
        if(empty($data['user_name'])){
            return ['msg'=>'学员姓名不能为空','code'=>0];
        }
        if(empty($data['identity_card'])){
            return ['msg'=>'学员身份证不能为空','code'=>0];
        }
        if(empty($data['phone'])){
            return ['msg'=>'学员号码不能为空','code'=>0];
        }
        if(empty($data['school_name'])){
            return ['msg'=>'学院名称不能为空','code'=>0];
        }
        if(empty($data['professional'])){
            return ['msg'=>'报考专业不能为空','code'=>0];
        }
        if(empty($data['address'])){
            return ['msg'=>'考试地址不能为空','code'=>0];
        }
        $data['order_sn'] = $this->createOrderNo();
        // 启动事务
        Db::startTrans();
        try {
            StudentModel::insert($data);
            BalanceModel::insertGetId(['students_sn'=>$data['order_sn'],'create_time'=>date('Y-m-d H:i:s',time())]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return  ['msg'=>$e->getMessage(),'code'=>0];
        }
        return ['msg'=>'添加成功','code'=>1];
    }
    //获取报考类型
    public static function type($pid){
        return Db::name("level")->where('pid',$pid)->value('name');
    }
    //生成订单编号
    private function createOrderNo(){
        return 'YS'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }





}